package org.HU.train.member.controller;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.HU.train.common.context.LoginMemberContext;
import org.HU.train.common.response.CommonResp;
import org.HU.train.common.response.PageResp;
import org.HU.train.member.request.passenger.PassengerQueryReq;
import org.HU.train.member.request.passenger.PassengerSaveReq;
import org.HU.train.member.response.PassengerQueryResp;
import org.HU.train.member.service.PassengerService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ZUZ
 * @Description: TODO
 * @Date 2024-12-20 15:50
 * @ClassName: PassengerController
 */
@RestController
@RequestMapping("/passenger")
public class PassengerController {

    @Resource
    private PassengerService passengerService;

    /**
     * 新增或更新乘车人
     */
    @PostMapping("/save")
    public CommonResp<Object> save(@Valid @RequestBody PassengerSaveReq req) {
        passengerService.save(req);
        return new CommonResp<>();
    }

    /**
     * 查询所有乘车人信息
     *
     * memberID由线程本地变量得到
     */
    @GetMapping("/query-list")
    public CommonResp<PageResp<PassengerQueryResp>> queryList(@Valid PassengerQueryReq req) {
        req.setMemberId(LoginMemberContext.getId());
        PageResp<PassengerQueryResp> list = passengerService.queryList(req);
        return new CommonResp<>(list);
    }

    /**
     * 删除某乘车人
     */
    @DeleteMapping("/delete/{id}")
    public CommonResp<Object> delete(@PathVariable Long id) {
        passengerService.delete(id);
        return new CommonResp<>();
    }

    /**
     * 查找所有乘客
     */
    @GetMapping("/query-mine")
    public CommonResp<List<PassengerQueryResp>> queryMine() {
        List<PassengerQueryResp> list = passengerService.queryMine();
        return new CommonResp<>(list);
    }

    /**
     * 初始化乘客，如果没有张三，就增加乘客张三，李四、王五同理，防止线上体验时乘客被删光
     */
    @GetMapping("/init")
    public CommonResp<Object> init() {
        passengerService.init();
        return new CommonResp<>();
    }

}
